package com.cci.kangdao.dao;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.mybatis.spring.annotation.MapperScan;

import java.util.List;
import java.util.Map;
@MapperScan
public interface SSHasCompanyDao {

    /**
     * 通过服务商Id查询主机厂授权信息
     * @param serviceStationId 服务商Id
     * @return 授权信息列表
     */
    @Select("select * from SSHasCompany_T where Flag = 0 and ServiceStationId = #{serviceStationId}")
    List<Map<String, Object>> selectSSHasCompanyByServiceStationId(Long serviceStationId);

    /**
     * 通过服务商Id和主机厂Id查询主机厂授权信息
     * @param map 主要使用companyId+serviceStationId
     * @return 授权信息
     */
    @Select("select * from SSHasCompany_T where Flag = 0 and CompanyId = #{companyId} and ServiceStationId = #{serviceStationId}")
    Map<String,Object> selectSSHasCompanyByCondition(Map<String,Object> map);

    /**
     * 保存主机厂授权信息
     * @param serviceStationId 服务商Id
     * @param companyId 主机厂Id
     */
    @Insert("insert into SSHasCompany_T (ServiceStationId, CompanyId, Flag, Systime) values (#{serviceStationId}, #{companyId}, default, sysdatetimeoffset())")
    void saveSSHasCompany(@Param("serviceStationId") Long serviceStationId, @Param("CompanyId") Long companyId);

    @Insert("insert SSHasCompany_T(ServiceStationId, CompanyId, Flag, Systime)\n" +
            "                  select #{serviceStationId},ID, 0, sysdatetimeoffset() from Company_T\n" +
            "                  where Flag = 0 and IsType between 4 and 5")
    void createAllRelation(@Param("serviceStationId") Long serviceStationId);

    /**
     * 通过服务商Id和主机厂Id作废授权记录
     * @param map 主要使用companyId+serviceStationId
     */
    @Update("update SSHasCompany_T set Flag = -1 where ServiceStationId = #{serviceStationId} and CompanyId = #{companyId} ")
    void deleteSSHasCompanyByServiceStationAndCompany(Map<String,Object> map);

    /**
     * 通过服务商Id作废授权记录
     * @param serviceStationId 服务商Id
     */
    @Update("update SSHasCompany_T set Flag = -1 where ServiceStationId = #{serviceStationId}")
    void deleteSSHasCompanyByServiceStationId(Long serviceStationId);

}